﻿using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace NopControls
{
    public static class Tools
    {
        public static double GetDoubleValue(TextBox box)
        {
            if (string.IsNullOrEmpty(box.Text))
                return 0;

            double result;

            if (!double.TryParse(box.Text, out result))
                return 0;

            return result;
        }

        public static Color GetColorFromPosition(double position)
        {
            const int gradientStops = 6;
            position *= gradientStops;
            byte mod = (byte)(position % 255);
            byte diff = (byte)(255 - mod);

            switch ((int)(position / 255))
            {
                case 0: return Color.FromArgb(255, 255, mod, 0);
                case 1: return Color.FromArgb(255, diff, 255, 0);
                case 2: return Color.FromArgb(255, 0, 255, mod);
                case 3: return Color.FromArgb(255, 0, diff, 255);
                case 4: return Color.FromArgb(255, mod, 0, 255);
                case 5: return Color.FromArgb(255, 255, 0, diff);
                case 6: return Color.FromArgb(255, 255, mod, 0);
                default: return Colors.Black;
            }
        }

        public static string GetHexadecimalValue(this Color c)
        {
            return string.Format("#{0}{1}{2}", c.R.ToString("X2"), c.G.ToString("X2"), c.B.ToString("X2"));
        }

        public static Color ToColor(this HSV hsv)
        {
            double h = hsv.Hue / 360;
            if (hsv.Saturation > 0)
            {
                if (h >= 1)
                    h = 0;
                h = 6 * h;
                int hueFloor = (int)Math.Floor(h);
                byte a = (byte)Math.Round(255 * hsv.Value * (1.0 - hsv.Saturation));
                byte b = (byte)Math.Round(255 * hsv.Value * (1.0 - (hsv.Saturation * (h - hueFloor))));
                byte c = (byte)Math.Round(255 * hsv.Value * (1.0 - (hsv.Saturation * (1.0 - (h - hueFloor)))));
                byte d = (byte)Math.Round(255 * hsv.Value);

                switch (hueFloor)
                {
                    case 0: return Color.FromArgb(255, d, c, a);
                    case 1: return Color.FromArgb(255, b, d, a);
                    case 2: return Color.FromArgb(255, a, d, c);
                    case 3: return Color.FromArgb(255, a, b, d);
                    case 4: return Color.FromArgb(255, c, a, d);
                    case 5: return Color.FromArgb(255, d, a, b);
                    default: return Color.FromArgb(0, 0, 0, 0);
                }
            }
            else
            {
                byte d = (byte)(hsv.Value * 255);
                return Color.FromArgb(255, d, d, d);
            }
        }

        public static HSV ToHSV(this Color c)
        {
            double r = (c.R / 255.0);
            double g = (c.G / 255.0);
            double b = (c.B / 255.0);

            double max = Math.Max(r, Math.Max(g, b));
            double min = Math.Min(r, Math.Min(g, b));

            double h = 0.0;
            if (max == r && g >= b)
            {
                h = 60 * (g - b) / (max - min);
            }
            else if (max == r && g < b)
            {
                h = 60 * (g - b) / (max - min) + 360;
            }
            else if (max == g)
            {
                h = 60 * (b - r) / (max - min) + 120;
            }
            else if (max == b)
            {
                h = 60 * (r - g) / (max - min) + 240;
            }

            if (double.IsNaN(h))
                h = 0;

            double s = (max == 0) ? 0.0 : (1.0 - (min / max));

            return new HSV(h, s, max);
        }

    }
}
